package com.atguigu.ssyx.activity.controller;


import com.atguigu.ssyx.activity.mapper.ActivityRuleMapper;
import com.atguigu.ssyx.activity.mapper.ActivitySkuMapper;
import com.atguigu.ssyx.activity.service.IActivityInfoService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.activity.ActivityInfo;
import com.atguigu.ssyx.model.activity.ActivityRule;
import com.atguigu.ssyx.model.activity.ActivitySku;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.vo.activity.ActivityRuleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * <p>
 * 操作活动表.
 * </p>
 *
 * @author author
 * @since 2024-06-30
 */
@RestController
@RequestMapping("/admin/activity/activityInfo")
public class ActivityInfoController {

    @Autowired
    private IActivityInfoService activityInfoService;

    @Autowired
    private ActivityRuleMapper activityRuleMapper;

    @Autowired
    private ActivitySkuMapper activitySkuMapper;

    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<ActivityInfo> pageParam = new Page<>(page, limit);
        IPage<ActivityInfo> pageModel = activityInfoService.selectPage(pageParam);
        return Result.ok(pageModel);
    }

    /**
     * 保存活动信息之后加上创建的时间。
     *
     *
     * @param activityInfo
     * @return
     */
    @ApiOperation(value = "新增活动")
    @PostMapping("save")
    public Result save(@RequestBody ActivityInfo activityInfo) {
        activityInfo.setCreateTime(new Date());
        activityInfoService.save(activityInfo);
        return Result.ok();
    }

    /*
     *
     * 根据活动id获得活动的规则和商品
     * */

    @ApiOperation(value = "获取活动规则")
    @GetMapping("findActivityRuleList/{id}")
    public Result findActivityRuleList(@PathVariable Long id) {
        Map<String, Object> map = activityInfoService.findActivityRuleList(id);
        return Result.ok(map);
    }

    /**
     * 在活动里面添加规则数据.
     */
    @PostMapping("saveActivityRule")
    public Result saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo) {
        activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok();
    }


    /**
     * 根据关键字获取sku列表，活动使用
     *
     * @param keyword
     * @return
     */
    @GetMapping("/findSkuInfoByKeyword/{keyword}")
    @ApiOperation(value = "填入数据,获得下拉框.")
    public Result findSkuInfoByKeyword(@PathVariable("keyword") String keyword) {
        List<SkuInfo> SkuInfo = activityInfoService.findSkuInfoByKeyword(keyword);
//        根据关键字查询sku的name.
        return Result.ok(SkuInfo);
    }


    @GetMapping("get/{id}")
    public Result findActivityRuleList1(@PathVariable Long id) {
        ActivityInfo byId = activityInfoService.getById(id);
        byId.setActivityTypeString(byId.getActivityType().getComment());
        return Result.ok(byId);
    }
}

